<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="artistName">
        <el-input v-model="queryParams.artistName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="国家" prop="country">
        <el-select v-model="form.country" placeholder="请选择国家" clearable style="width: 100%;">
          <el-option label="中国" value="中国"></el-option>
          <el-option label="美国" value="美国"></el-option>
          <el-option label="英国" value="英国"></el-option>
          <el-option label="法国" value="法国"></el-option>
          <el-option label="德国" value="德国"></el-option>
          <el-option label="日本" value="日本"></el-option>
          <el-option label="韩国" value="韩国"></el-option>
          <el-option label="其他" value="其他"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年代" prop="livingEra">
        <el-select v-model="queryParams.livingEra" placeholder="请选择年代" clearable style="width: 100%;">
          <el-option label="古代" value="古代"></el-option>
          <el-option label="近代" value="近代"></el-option>
          <el-option label="现代" value="现代"></el-option>
          <el-option label="当代" value="当代"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['gallery:artist:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['gallery:artist:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['gallery:artist:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['gallery:artist:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="artistList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="artistName" />
      <el-table-column label="头像" align="center" prop="avatar" width="120">
        <template slot-scope="scope">
          <el-image :src="scope.row.avatar" fit="cover" style="width: 50px; height: 50px; border-radius: 50%;"
            :preview-src-list="[scope.row.avatar]">
            <div slot="error" class="image-slot">
              <i class="el-icon-user-solid"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="简介" align="center" prop="bio" min-width="200">
        <template slot-scope="scope">
          <div class="bio-cell">
            <div class="bio-content-wrapper">
              <span v-html="parseBioContent(scope.row.bio)"></span>
              <span v-if="scope.row.bio && scope.row.bio.length > 15" class="show-more" @click="showFullBio(scope.row)">
                展示更多
              </span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="国家" align="center" prop="country" />
      <el-table-column label="年代" align="center" prop="livingEra" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['gallery:artist:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['gallery:artist:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改艺术家对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="small">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="姓名" prop="artistName">
              <el-input v-model="form.artistName" placeholder="请输入艺术家姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年代" prop="livingEra">
              <el-select v-model="form.livingEra" placeholder="请选择年代" clearable style="width: 100%;">
                <el-option label="古代" value="古代"></el-option>
                <el-option label="近代" value="近代"></el-option>
                <el-option label="现代" value="现代"></el-option>
                <el-option label="当代" value="当代"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="国家" prop="country">
              <el-select v-model="form.country" placeholder="请选择国家" clearable style="width: 100%;">
                <el-option label="中国" value="中国"></el-option>
                <el-option label="美国" value="美国"></el-option>
                <el-option label="英国" value="英国"></el-option>
                <el-option label="法国" value="法国"></el-option>
                <el-option label="德国" value="德国"></el-option>
                <el-option label="日本" value="日本"></el-option>
                <el-option label="韩国" value="韩国"></el-option>
                <el-option label="其他" value="其他"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态" style="width: 100%;">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="头像" prop="avatar">
              <image-upload v-model="form.avatar" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="简介" prop="bio">
              <editor v-model="form.bio" :min-height="192" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入备注信息" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 完整简介对话框 -->
    <el-dialog :title="fullBioDialog.title" :visible.sync="fullBioDialog.visible" width="600px" append-to-body>
      <div v-html="fullBioDialog.content" class="full-bio-content"></div>
    </el-dialog>
  </div>
</template>

<script>
import { listArtist, getArtist, delArtist, addArtist, updateArtist } from "@/api/gallery/artist"

export default {
  name: "Artist",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 艺术家表格数据
      artistList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        artistName: null,
        avatar: null,
        bio: null,
        country: null,
        livingEra: null,
        livingEra: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        artistName: [
          { required: true, message: "艺术家姓名不能为空", trigger: "blur" }
        ],
      },
      // 添加控制简介显示的变量
      fullBioDialog: {
        visible: false,
        content: '',
        title: '简介详情'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询艺术家列表 */
    getList() {
      this.loading = true
      listArtist(this.queryParams).then(response => {
        this.artistList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        artistId: null,
        artistName: null,
        avatar: null,
        bio: null,
        country: null,
        livingEra: null,
        livingEra: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.artistId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加艺术家"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const artistId = row.artistId || this.ids
      getArtist(artistId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改艺术家"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.artistId != null) {
            updateArtist(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addArtist(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const artistIds = row.artistId || this.ids
      this.$modal.confirm('是否确认删除艺术家编号为"' + artistIds + '"的数据项？').then(function () {
        return delArtist(artistIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gallery/artist/export', {
        ...this.queryParams
      }, `artist_${new Date().getTime()}.xlsx`)
    },
    /** 显示完整简介 */
    showFullBio(row) {
      this.fullBioDialog.content = row.bio;
      this.fullBioDialog.visible = true;
    },
    /** 解析简介内容，去除HTML标签并按规范截取 */
    parseBioContent(bio) {
      if (!bio) return '';
      // 去除HTML标签
      const text = bio.replace(/<[^>]+>/g, '');
      // 按规范处理长度：不超过15位直接显示；超过15位则显示前10位和后5位，中间用省略号连接
      if (text.length <= 50) return text;
      return text.substring(0, 50) + '...';
    }
  }
}
</script>

<style scoped>
.bio-content {
  text-align: left;
  cursor: pointer;
  color: #409EFF;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dialog-footer {
  text-align: right;
  padding: 15px 20px;
  border-top: 1px solid #e8eaec;
}

.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 20px;
}

.bio-cell {
  text-align: left;
}

.bio-content-wrapper {
  display: flex;
  align-items: center;
  text-align: left;
}

.bio-content-wrapper span:first-child {
  flex: 1;
}

.show-more {
  color: #409EFF;
  cursor: pointer;
  margin-left: 10px;
  white-space: nowrap;
}

.full-bio-content {
  text-align: left;
  line-height: 1.6;
}
</style>
